<?php
class service_category{
	static public function getAll($type){
		$category = new DaoCategory();
		$cates = array();
		$cates = $category->getAll("typeID = '$type'");
		return $cates;
	}
	//获取顶级分类(parentID=0)
	static public function getTopCate($type){
		$category = new DaoCategory();
		$topCate = array();
		$topCate = $category->getAll(" parentID = '0' and typeID = '$type'");
		return $topCate;
	}
	//查看是否有下级分类，如果有返回true,否则返回false
	static public function hasChildren($parentID){
		$category = new DaoCategory();
		$cate = $category->getAll("parentID = '$parentID'");
		if(empty($cate)){
			return false;
		}else{
			return true;
		}
	}
	
	static public function getOne($cateID){
		$category = new DaoCategory();
		$cate = $category->getOne("id = '$cateID'");
		return $cate;
	}
	/**
	  *通过ID获取子分类
	  *@parentID	分类ID
	  *$cate 返回自己子分类，没有子分类返回空数组
	  */
	static public function getChildren($parentID){
		$category = new DaoCategory();
		$cate = array();
		$cate = $category->getAll(" parentID = '$parentID'");
		return $cate;
	}
	static public function getNameByID($cateID){
		$category = new DaoCategory();
		$cate = $category->getOne(" id = '$cateID'");
		return empty($cate)?'':$cate['title'];
	}
	static function getCateOption($cate,$parentID,$cateID,$show=0){
	if(!($cateID == $cate['id'])){
		$str = "<option value='".$cate['id']."' ";
		if($cate['id']==$parentID){
			$str .= " selected='selected' ";
		}
		$num = $show?substr_count($cate['path'],','):substr_count($cate['path'],',')-1;
		$str .= " >".str_repeat('&ndash;',$num).$cate['title']."</option>";
		echo $str;
		$children = service_category::getChildren($cate['id']);
		if(!empty($children)){
			foreach($children as $child){
				self::getCateOption($child,$parentID,$cateID,$show);
			}
		}
	}
}
}
?>
